const Koa = require('koa'); //Koa框架
const views = require('koa-views')//渲染模板中间件
const path = require('path')
const bodyParser = require('koa-bodyparser')//解析body中的数据
const session = require('koa-session-minimal');//session
const MysqlStore = require('koa-mysql-session');//mysql session
const config = require('./config/default.js');//全局配置
const app = new Koa(); //生成koa 实列
app.use(bodyParser()) 

// session存储配置
const sessionMysqlConfig= {
  user: config.database.USERNAME,
  password: config.database.PASSWORD,
  database: config.database.DATABASE,
  host: config.database.HOST,
}
// 配置session中间件
app.use(session({
  key: 'USER_SID',
  store: new MysqlStore(sessionMysqlConfig)
}))

// 配置服务端模板渲染引擎中间件
app.use(views(path.join(__dirname, './views'), {extensions: ['html']})) //渲染 views 目录下的 静态页面

//路由文件
//注册
app.use(require('./routers/register.js').routes())
//登录
app.use(require('./routers/login.js').routes())
//home
app.use(require('./routers/home.js').routes())

//监听端口
app.listen(3003, () => {
  console.log(`app running on port 3003`)
})


